package com.example.sms_emoticon_library;


import java.io.FileNotFoundException;

import com.example.model.*;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;



public class PictureSms extends Activity {

	ImageView imgPicture;
	TextView txtSend,txtBack;
	EditText edtText;
	Intent intentFuntion;
	Bundle dataLoadImage;
	Bitmap scaled, bmSmsPicture;
	String picturePath;
	int finalHeight, finalWidth;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_picture_sms);
		imgPicture = (ImageView) findViewById(R.id.imgPictureSms);
	    txtSend = (TextView) findViewById(R.id.txtSend);
	    txtBack = (TextView) findViewById(R.id.txtBack);
	    edtText = (EditText) findViewById(R.id.edtText);
	    
	    ViewTreeObserver VTO= imgPicture.getViewTreeObserver();
	    VTO.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
			
			@Override
			public boolean onPreDraw() {
				// TODO Auto-generated method stub
				finalHeight= imgPicture.getMeasuredHeight();
				finalWidth= imgPicture.getMeasuredWidth();
				
				return true;
			}
		});
	    Toast.makeText(this, "Height "+ finalHeight, Toast.LENGTH_SHORT).show();
		intentFuntion= getIntent();
		dataLoadImage=getIntent().getExtras();
		
		// bmSmsPicture= dataLoadImage.getParcelable("abc");
		
		//String encodedImage= dataLoadImage.getString("encodedImage");
		//byte[] a = encodedImage.getBytes();
		//BitmapFactory.Options bo= new BitmapFactory.Options();
		//bo.inSampleSize=2;
		//Bitmap image= BitmapFactory.decodeByteArray(a, 0, a.length,bo);
		
		picturePath= dataLoadImage.getString("picturePath");
		//bmSmsPicture= BitmapFactory.decodeFile(picturePath);
		
		//float a= getResources().getDisplayMetrics().density;
		//float width = imgPicture.getDisplay().getWidth()*a;
		//float height= imgPicture.getHeight()*a;
		//int iwidth= (int) width;
		//int iheight= (int) height;
		bmSmsPicture = decodeSampledBitmapFromPath(picturePath, 250 , 200);
		//int nh = (int) ( bmSmsPicture.getHeight() * (512.0 / bmSmsPicture.getWidth()) );
		//scaled = Bitmap.createScaledBitmap(bmSmsPicture, 512, nh, true);
		//Uri myUri = Uri.parse(picturePath);
		imgPicture.setImageBitmap(bmSmsPicture);
		
		
		
		txtSend.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			   SmsPicture smsPic =  new SmsPicture();
			   smsPic.set_bitmap(bmSmsPicture);
			   smsPic.set_text(edtText.getText().toString());
			   ParcelableSmsPicture parSmsPic= new ParcelableSmsPicture(smsPic);
			   Bundle  bund = new Bundle();
			   
			   //bund.putSerializable("SmsPic", (Serializable) smsPic);
			   //bund.putParcelable("ParSmsPic", parSmsPic);
			   bund.putString("text", edtText.getText().toString());
			   bund.putString("path", picturePath);
			   intentFuntion.putExtra("DATA", bund);
			   setResult(FragmentFuntions.RESULT_SENT_PIC, intentFuntion);
			   finish();
			   
			  
			
			}
		});
	}

	
	


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.picture_sms, menu);
		return true;
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		
	}
	
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	    //scaled.recycle();
	}
	
	public Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
            int reqHeight) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

      
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        Bitmap bmp = BitmapFactory.decodeFile(path, options);
        return bmp;
        }

    public int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {

        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float) height / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
             }
         }
         return inSampleSize;
        }


	private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(
                getContentResolver().openInputStream(selectedImage), null, o);

        final int REQUIRED_SIZE = 100;

        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(
                getContentResolver().openInputStream(selectedImage), null, o2);
    }
	
	

}
